package service;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.urlfetch.HTTPResponse;

import controller.Utils;

public class GetContent{
	public String Get(Future future,int i,String domain)
	{
		String str = "";
		
		HTTPResponse response;
		try {
			response = (HTTPResponse) future.get();
		
	        Document doc = Jsoup.parse(new String(response.getContent(),"utf-8"));
//			System.out.println(doc);
			Elements cite = doc.select(".g");
			if(cite.size() > 0)
			{
				for (int j = 0; j < cite.size(); j++) {
					if(j < 10)
					{
						if(Utils.fomatDomain(cite.get(j).select("a").attr("href").replace("/url?q=", "")).equals(domain))
						{
							str = cite.get(j).select("a").attr("href").replace("/url?q=", "")+"|"+i+"|"+j;
							System.out.println(str);
						}
					}
				}
			}
			

        } catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ExecutionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return str;
	}
//	public static void main(String[] args) throws IOException {
//		Content("gogo","www.google.com.vn");
//	}
}
